package com.lagou.edu.utils;

import java.io.File;

/**
 * 文件操作工具
 *
 * @author liangzj
 * @date 2021/4/10 0:56
 */
public class FileUtil {

    /**
     * 搜索指定名称的文件
     *
     * @param dirFile  搜索起始目录
     * @param filename 文件名
     * @return
     */
    public static File[] searchFileByName(File dirFile, String filename) {
        File[] files = dirFile.listFiles((dir, name) -> filename.equals(name));
        if (files != null && files.length > 0) {
            return files;
        }

        File[] childDirFiles = dirFile.listFiles();
        if (childDirFiles != null && childDirFiles.length > 0) {
            for (File childDirFile : childDirFiles) {
                File[] subFiles = searchFileByName(childDirFile, filename);
                if (subFiles != null && subFiles.length > 0) {
                    return subFiles;
                }
            }
        }

        return null;
    }
}
